package priv.abadstring.study.haoke.im.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import priv.abadstring.study.haoke.im.bean.Message;
import priv.abadstring.study.haoke.im.dao.MessageDAO;

import java.util.List;

/**
 * MessageController
 *
 * @author aBadString
 * @date 2021-07-30 19:16:58
 */
@RestController
@CrossOrigin
public class MessageController {
    @Autowired
    private MessageDAO messageDAO;

    /**
     * 获取历史消息列表
     */
    @GetMapping("message")
    public List<Message> queryMessageList(
            @RequestParam("fromId") Long fromId,
            @RequestParam("toId") Long toId,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "10") Integer rows) {

        List<Message> list = messageDAO.findListByFromAndTo(fromId, toId, page, rows);
        for (Message message : list) {
            if(message.getStatus() == 1){
                // 修改消息状态为已读
                this.messageDAO.updateMessageState(message.getId(), 2);
            }
        }
        return list;
    }

}
